package cn.tedu.gateway.predicate;


import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

//@Component
public class AutoCheckRoutePredicateFactory extends
        AbstractRoutePredicateFactory<AutoCheckRoutePredicateFactory.Config>{

//构造器 搞定
    public AutoCheckRoutePredicateFactory() {
        super(Config.class);
    }
    //参数转换
    public List<String> shortcutFieldOrder(){
        return Arrays.asList("name");
    }


//加载配置参数 判断是否满足条件   name = tom
    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
       return new GatewayPredicate() {
          //判断条件  满足或者不满足  true  false
           @Override
           public boolean test(ServerWebExchange serverWebExchange) {
               //从yml文件获取AutoCheck 断言的值
               if (config.getName().equals("tom")){
                   return true;
               }
               return false;
           }
       };
    }

    public static class Config{
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
